package com.example.onlinemusic01.Controller;

import com.example.onlinemusic01.Common.AjaxResponse;
import com.example.onlinemusic01.Mapper.UserMapper;
import com.example.onlinemusic01.Model.User;
import com.example.onlinemusic01.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
   @Autowired
   private UserService userService;

    @RequestMapping("/login")
    public Object login(String username, String password, HttpServletRequest request) {
        System.out.println(username+" "+password);
        if (!StringUtils.hasLength(username) && !StringUtils.hasLength(password)) {
            return AjaxResponse.fail(-1, "缺少必要信息");
        }
        HttpSession session = request.getSession(true);
        User user = userService.login(username);
        if (user == null) {
            return AjaxResponse.fail(-1, "用户名或密码不正确");
        } else if (!username.equals(user.getUsername())) {
            return AjaxResponse.fail(-1, "用户名或密码不正确");
        } else {
            return AjaxResponse.success("登陆成功");
        }
    }

}
